﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication04API.Results;
using WebApplication04API.Models;
using System.Collections;

namespace WebApplication04API.Controllers
{
    public class ValuesController : ApiController
    {
       
        [HttpGet]
        public Result test()
        {
            return new Result("success", "ok");
        }
        
        Session3Entities en = new Session3Entities();

        [HttpGet]
        public Result findByAllPMTest(string assetName,string taskName)
        {
            var item = en.PMTasks.OrderBy(o => o.TaskDone == true).ThenBy(o =>o.PMScheduleTypeID==2).ThenBy(o=>o.ScheduleDate).ThenBy(o=>o.ScheduleKilometer).ToList();

            if(assetName != null)
            {
                item = item.Where(o => o.Asset.AssetName == assetName).ToList();
            }
            if(taskName != null)
            {
                item = item.Where(o => o.Task.Name == taskName).ToList();
            }
            List<Hashtable> list = new List<Hashtable>();
            foreach(var items in item)
            {
                Hashtable h = new Hashtable();
                h.Add("PMID", items.ID);
                h.Add("AssetName", items.Asset.AssetName);
                h.Add("AssetSN", items.Asset.AssetSN);
                h.Add("TaskName", items.Task.Name);
                h.Add("PMScheduleType", items.PMScheduleType.Name);
                h.Add("ScheduleKilometer", items.ScheduleKilometer);
              

                h.Add("ScheduleDate", items.ScheduleDate);
                h.Add("TaskDone", items.TaskDone);
                list.Add(h);
            }
            return new Result("success",list);
        }

        [HttpGet]
        public Result findByAllPM(string assetName, string taskName,string date)
        {
            var item = en.PMTasks.OrderBy(o => o.TaskDone == true).ThenBy(o => o.PMScheduleTypeID == 2).ThenBy(o => o.ScheduleDate).ThenBy(o => o.ScheduleKilometer).ToList();

            if (assetName != null)
            {
                item = item.Where(o => o.Asset.AssetName == assetName).ToList();
            }
            if (taskName != null)
            {
                item = item.Where(o => o.Task.Name == taskName).ToList();
            }
            if(date != null)
            {
                item = item.Where(o => o.ScheduleDate <= DateTime.Parse(date)).ToList();
            }
            List<Hashtable> list = new List<Hashtable>();
            foreach (var items in item)
            {
                Hashtable h = new Hashtable();
                h.Add("PMID", items.ID);
                h.Add("AssetName", items.Asset.AssetName);
                h.Add("AssetSN", items.Asset.AssetSN);
                h.Add("TaskName", items.Task.Name);
                h.Add("PMScheduleType", items.PMScheduleType.Name);
                h.Add("ScheduleKilometer", items.ScheduleKilometer);
                //[ScheduleKilometer]
                //en.PMScheduleModels.Where(o => o.Sc)
                // h.Add("ScheduleModel",)

                h.Add("ScheduleDate", items.ScheduleDate);
                h.Add("TaskDone", items.TaskDone);
                list.Add(h);
            }
            return new Result("success", list);
        }

        [HttpGet]
        public Result updateIsTaskDone(int PMID,Boolean taskDone)
        {
            var item = en.PMTasks.Where(o => o.ID == PMID).First();
            if(item != null)
            {
                item.TaskDone = taskDone;
            }
            en.SaveChanges();
            return new Result("success", "");
        }

        [HttpGet]
        public Result insertPM(string assetName,string taskName,string scheduleModel,string date,string km)
        {
            long assetId = en.Assets.Where(o => o.AssetName == assetName).FirstOrDefault().ID;
            long taskId = en.Tasks.Where(o => o.Name == taskName).FirstOrDefault().ID;
            //注意：PMScheduleTypeID
            var scheduleModelId = en.PMScheduleModels.Where(o => o.Name.Equals(scheduleModel)).First().PMScheduleTypeID;
           
            PMTask pMTask = new PMTask();
            
            pMTask.TaskID = taskId;
            pMTask.AssetID = assetId;
            pMTask.PMScheduleTypeID = (long)scheduleModelId;
            if(date != null)
            {
                pMTask.ScheduleDate = DateTime.Parse(date);
            }
            else
            {
                pMTask.ScheduleDate = null;
            }
            if(km.Equals("0"))
            {
                pMTask.ScheduleKilometer = null;
            }
            else
            {
                //pMTask.ScheduleKilometer = (long)int.Parse(km);
                pMTask.ScheduleKilometer = long.Parse(km);
            }

            pMTask.TaskDone = false;

            en.PMTasks.Add(pMTask);
            en.SaveChanges();
            return new Result("success","");
        }
    }
}
